package com.androidmvc.base.utils.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtils {
    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    public static String getStandardDate(long timeStr) {
        long diff = new Date().getTime() - timeStr;
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    public static String ToTimeString(long ticket) {
        return new SimpleDateFormat("yyyy-MM-dd")
                .format(new Date(ticket));
    }

    public static String ToTimeYearAndTime(long ticket) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm")
                .format(new Date(ticket));
    }

    public static String ToTimeHour(long ticket) {
        return new SimpleDateFormat("HH:mm").format(new Date(ticket))
                .split(":")[0];
    }

    /**
     * String类型转换为Date
     *
     * @param currTime
     * @param formatType
     * @return
     */

    public static Date stringToDate(String currTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = formatter.parse(currTime);
        return date;
    }

    /**
     * Date类型转换为String
     */

    public static String dateToString(Date data, String formatType) {

        return new SimpleDateFormat(formatType).format(data);

    }

    /**
     * long类型转换为Date类型
     */

    public static Date longToDate(long currTime, String formatType)
            throws ParseException {

        Date old = new Date(currTime);

        String time = dateToString(old, formatType);

        Date date = stringToDate(time, formatType);

        return date;

    }

    /**
     * long类型时间转换为String类型
     */

    public static String longToString(long currTime, String formatType)
            throws ParseException {

        Date date = longToDate(currTime, formatType);

        String strTime = dateToString(date, formatType);

        return strTime;

    }

    /**
     * Date类型转换为long
     */

    public static long dateToLong(Date date) {

        return date.getTime();

    }

    /**
     * String类型转换为long
     */

    public static long stringToLong(String strTime, String formatType)

            throws ParseException {

        Date date = stringToDate(strTime, formatType); // String类型转成date类型

        if (date == null) {

            return 0;

        } else {

            long currentTime = dateToLong(date); // date类型转成long类型

            return currentTime;

        }

    }

    /**
     * 获取当天string类型的日期
     *
     * @param count
     * @return
     */
    public static String getSelectDay(int count, String formatType) {
        Date now = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(now);
        calendar.add(calendar.DATE, count);// 把日期往后增加一天.整数往后推,负数往前移动
        now = calendar.getTime();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(formatType);
        return format.format(now);
    }


    public static Calendar getCalendarSelectDay(int count) {
        Date now = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(now);
        calendar.add(calendar.DATE, count);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar;
    }

    /**
     * 把当然Date时间往后推几天
     *
     * @param date
     * @param count
     * @return
     */
    public static Date getDateSelectDay(Date date, int count) {
        Date now = date;
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(now);
        calendar.add(calendar.DATE, count);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime();
    }

    /**
     * 活动倒计时
     *
     * @param date 活动发布时间  ntime  服务器当前时间
     * @param days
     * @return
     * @throws ParseException
     */
    public static String getHuoDongDay(Date date, long ntime, int days) throws ParseException {
        long lastTime = (ntime - date.getTime()) / 1000;
        long allTime = days * 60 * 60 * 24;    //标准算法
//        long allTime = days * 60;                //测试算法
        long remainTime = allTime - lastTime;
        int hour = (int) remainTime / (60 * 60);
        int min = (int) (remainTime - (hour * (60 * 60))) / 60;
        String s = String.format("%d小时%d分", hour < 0 ? 0 : hour, min < 0 ? 0 : min);
        return s;
    }

    /**
     * 获取是周几
     *
     * @param calendar
     * @return
     */
    public static String getCurrentWeekOfMonth(Calendar calendar) {
        String strWeek = "";
        int dw = calendar.get(Calendar.DAY_OF_WEEK);
        if (dw == 1) {
            strWeek = "周天";
        } else if (dw == 2) {
            strWeek = "周一";
        } else if (dw == 2) {
            strWeek = "周二";
        } else if (dw == 2) {
            strWeek = "周三";
        } else if (dw == 2) {
            strWeek = "周四";
        } else if (dw == 2) {
            strWeek = "周五";
        }
        return strWeek;
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     * 返回今天是周几
     *
     * @return
     */
    public static Integer getWeek() {
        Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     * 获取当天0点时间
     *
     * @return
     */
    public static Date getTimeOf12() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 0);
        return cal.getTime();
    }

    /**
     * 两个日期进行相减得到天数
     *
     * @param begin_date
     * @param end_date
     * @return
     */
    public static long getTwoDay(Date begin_date, Date end_date) {
        long day = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String sdate = format.format(Calendar.getInstance().getTime());

            if (begin_date == null) {
                begin_date = format.parse(sdate);
            }
            if (end_date == null) {
                end_date = format.parse(sdate);
            }
            day = (end_date.getTime() - begin_date.getTime())
                    / (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            return -1;
        }
        return day;
    }

    public static String dateDiff(String startTime, String endTime, String format) {
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数
        long diff;
        try {
            //获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            long day = diff / nd;//计算差多少天
            long hour = diff % nd / nh;//计算差多少小时
            long min = diff % nd % nh / nm;//计算差多少分钟
            long sec = diff % nd % nh % nm / ns;//计算差多少秒
            return day + "a" + hour + "a" + min;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 同一天进行相减得到分钟数
     *
     * @param begin_date
     * @param end_date
     * @return
     */
    public static long getTwoMinute(Date begin_date, Date end_date) {
        long day = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String sdate = format.format(Calendar.getInstance().getTime());

            if (begin_date == null) {
                begin_date = format.parse(sdate);
            }
            if (end_date == null) {
                end_date = format.parse(sdate);
            }
            day = (end_date.getTime() - begin_date.getTime())
                    / (60 * 60 * 1000);
        } catch (Exception e) {
            return -1;
        }
        return day;
    }


    /**
     * 两个日期进行相减得到天数 ，使用自然天计算
     *
     * @param early
     * @param late
     * @return
     */
    public static final int daysBetween(Date early, Date late) {
        java.util.Calendar calst = java.util.Calendar.getInstance();
        java.util.Calendar caled = java.util.Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        //设置时间为0时
        calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
        calst.set(java.util.Calendar.MINUTE, 0);
        calst.set(java.util.Calendar.SECOND, 0);
        caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
        caled.set(java.util.Calendar.MINUTE, 0);
        caled.set(java.util.Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
        return days;
    }


}
